//
// Created by kevin on 2024/11/26.
//
#include <cmath>
#include "lte_getofdminfo.h"

unsigned int Nfft_table_normalCP[5][14] = {{160, 144, 144, 144, 144, 144, 144, 160, 144, 144, 144, 144, 144, 144},
                                           {80, 72, 72 ,72, 72, 72, 72, 80, 72, 72, 72, 72, 72, 72},
                                           {40, 36, 36 ,36, 36, 36, 36, 40, 36, 36, 36, 36, 36, 36},
                                           {20, 18 ,18 ,18 ,18, 18, 18, 20 ,18, 18, 18, 18, 18, 18},
                                           {10, 9, 9, 9, 9, 9, 9, 10, 9, 9, 9, 9, 9, 9}};
unsigned int Nfft_table_extendCP[5][12] = {{512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512},
                                           {256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256},
                                           {128 ,128 ,128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
                                           {64 ,64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
                                           {32 ,32, 32 ,32, 32, 32, 32, 32, 32, 32, 32 ,32}};


sLteOfdmInfo lte_getofdminfo(unsigned int RB,const std::string CPtype){

    int res = CPtype.compare("Normal");
    if(res != 0){
        std::cout << "extended cp is not supported now"<< std::endl;
    }


    // FFT size
    /*
    unsigned int Nfft_table_normalCP[5][14] = {{160, 144, 144, 144, 144, 144, 144, 160, 144, 144, 144, 144, 144, 144},
                                               {80, 72, 72 ,72, 72, 72, 72, 80, 72, 72, 72, 72, 72, 72},
                                               {40, 36, 36 ,36, 36, 36, 36, 40, 36, 36, 36, 36, 36, 36},
                                               {20, 18 ,18 ,18 ,18, 18, 18, 20 ,18, 18, 18, 18, 18, 18},
                                               {10, 9, 9, 9, 9, 9, 9, 10, 9, 9, 9, 9, 9, 9}};
    unsigned int Nfft_table_extendCP[5][12] = {{512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512, 512},
            {256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256},
            {128 ,128 ,128, 128, 128, 128, 128, 128, 128, 128, 128, 128},
            {64 ,64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64},
            {32 ,32, 32 ,32, 32, 32, 32, 32, 32, 32, 32 ,32}};
    */


    // FFT table
    unsigned int FFT_table[5] = {2048, 1024, 512, 256, 128};

    double RE_all = 12 * RB;
    unsigned int Nfft = 0;
    {
        double res = log2(RE_all);
        double cres = ceil(res);
        Nfft = pow(2,cres);
    }
    unsigned int idx = 0;
    for(int i = 0; i < 5; i++){
        if(Nfft == FFT_table[i]){
            idx = i;
        }
    }


    // normal
    unsigned int *pCP = NULL;
    double samplingrate = 0.0;
    if(res == 0){
        pCP = &Nfft_table_normalCP[idx][0];
        unsigned int sum = 0;
        for(int i = 0; i < 14; i++){
            sum += pCP[i];
        }

        unsigned int nsample = 14*Nfft + sum;
        samplingrate = nsample / 1e-3;
    }

    res = CPtype.compare("Extend");
    if(res == 0){
        pCP = &Nfft_table_extendCP[idx][0];
        unsigned int sum = 0;
        for(int i = 0; i < 14; i++){
            sum += pCP[i];
        }

        unsigned int nsample = 12*Nfft + sum;
        samplingrate = nsample / 1e-3;
    }

    sLteOfdmInfo lteofdminfo;

    lteofdminfo.Nfft = Nfft;
    lteofdminfo.CPsize = pCP;
    lteofdminfo.SamplingRate = samplingrate;

    return lteofdminfo;
}
